home *** CD-ROM | disk | FTP | other *** search
/ Java Primer Plus / Java Primer Plus (Waite Group Proess)(1996).iso / java_Win / demo / ImageMap / ButtonFilter.class (.txt) next >
Encoding:
Java Class File  |  1995-10-12  |  4.1 KB  |  178 lines

  1. import java.awt.image.ColorModel;
  2. import java.awt.image.IndexColorModel;
  3. import java.awt.image.RGBImageFilter;
  4.  
  5. class ButtonFilter extends RGBImageFilter {
  6.    boolean pressed;
  7.    int defpercent;
  8.    int border;
  9.    int width;
  10.    int height;
  11.    ColorModel[] models = new ColorModel[7];
  12.    ColorModel origbuttonmodel;
  13.  
  14.    public ButtonFilter(boolean press, int p, int b, int w, int h) {
  15.       this.pressed = press;
  16.       this.defpercent = p;
  17.       this.border = b;
  18.       this.width = w;
  19.       this.height = h;
  20.    }
  21.  
  22.    public void setHints(int hints) {
  23.       super.setHints(hints & -5);
  24.    }
  25.  
  26.    public void setColorModel(ColorModel model) {
  27.       if (model instanceof IndexColorModel) {
  28.          IndexColorModel icm = (IndexColorModel)model;
  29.          this.models[0] = this.filterIndexColorModel(icm, false, false, 0);
  30.          this.models[1] = this.filterIndexColorModel(icm, true, !this.pressed, this.defpercent);
  31.          this.models[2] = null;
  32.          if (this.pressed) {
  33.             this.models[3] = this.filterIndexColorModel(icm, true, false, this.defpercent / 2);
  34.          } else {
  35.             this.models[3] = this.models[0];
  36.          }
  37.  
  38.          this.models[4] = null;
  39.          this.models[5] = this.filterIndexColorModel(icm, true, this.pressed, this.defpercent);
  40.          this.models[6] = this.models[0];
  41.          this.origbuttonmodel = model;
  42.          super.consumer.setColorModel(this.models[3]);
  43.       } else {
  44.          super.setColorModel(model);
  45.       }
  46.    }
  47.  
  48.    public IndexColorModel filterIndexColorModel(IndexColorModel icm, boolean opaque, boolean brighter, int percent) {
  49.       byte[] r = new byte[256];
  50.       byte[] g = new byte[256];
  51.       byte[] b = new byte[256];
  52.       byte[] a = new byte[256];
  53.       int mapsize = icm.getMapSize();
  54.       icm.getReds(r);
  55.       icm.getGreens(g);
  56.       icm.getBlues(b);
  57.       if (opaque) {
  58.          icm.getAlphas(a);
  59.  
  60.          for(int i = 0; i < mapsize; ++i) {
  61.             int rgb = this.filterRGB(icm.getRGB(i), brighter, percent);
  62.             a[i] = (byte)(rgb >> 24);
  63.             r[i] = (byte)(rgb >> 16);
  64.             g[i] = (byte)(rgb >> 8);
  65.             b[i] = (byte)rgb;
  66.          }
  67.       }
  68.  
  69.       return new IndexColorModel(((ColorModel)icm).getPixelSize(), mapsize, r, g, b, a);
  70.    }
  71.  
  72.    public void buttonRanges(int y, int[] ranges) {
  73.       ranges[0] = ranges[1] = 0;
  74.       if (y < this.border) {
  75.          ranges[2] = ranges[3] = ranges[4] = ranges[5] = this.width - y;
  76.       } else if (y > this.height - this.border) {
  77.          ranges[2] = ranges[3] = ranges[4] = ranges[5] = this.height - y;
  78.       } else {
  79.          ranges[2] = ranges[3] = this.border;
  80.          ranges[4] = ranges[5] = this.width - this.border;
  81.       }
  82.  
  83.       ranges[6] = ranges[7] = this.width;
  84.    }
  85.  
  86.    public void setPixels(int x, int y, int w, int h, ColorModel model, byte[] pixels, int off, int scansize) {
  87.       if (model != this.origbuttonmodel) {
  88.          super.setPixels(x, y, w, h, model, pixels, off, scansize);
  89.       } else {
  90.          int[] ranges = new int[8];
  91.          int x2 = x + w;
  92.  
  93.          for(int cy = y; cy < y + h; ++cy) {
  94.             this.buttonRanges(cy, ranges);
  95.  
  96.             for(int i = 0; i < 7; ++i) {
  97.                if (x2 > ranges[i] && x < ranges[i + 1]) {
  98.                   int cx1 = Math.max(x, ranges[i]);
  99.                   int cx2 = Math.min(x2, ranges[i + 1]);
  100.                   if (this.models[i] == null) {
  101.                      super.setPixels(cx1, cy, cx2 - cx1, 1, model, pixels, off + (cx1 - x), scansize);
  102.                   } else if (cx1 < cx2) {
  103.                      super.consumer.setPixels(cx1, cy, cx2 - cx1, 1, this.models[i], pixels, off + (cx1 - x), scansize);
  104.                   }
  105.                }
  106.             }
  107.  
  108.             off += scansize;
  109.          }
  110.  
  111.       }
  112.    }
  113.  
  114.    public int filterRGB(int x, int y, int rgb) {
  115.       boolean brighter;
  116.       int percent;
  117.       if ((x >= this.border || y >= this.height - x) && (y >= this.border || x >= this.width - y)) {
  118.          if (x < this.width - this.border && y < this.height - this.border) {
  119.             if (!this.pressed) {
  120.                return rgb & 16777215;
  121.             }
  122.  
  123.             brighter = false;
  124.             percent = this.defpercent / 2;
  125.          } else {
  126.             brighter = this.pressed;
  127.             percent = this.defpercent;
  128.          }
  129.       } else {
  130.          brighter = !this.pressed;
  131.          percent = this.defpercent;
  132.       }
  133.  
  134.       return this.filterRGB(rgb, brighter, percent);
  135.    }
  136.  
  137.    public int filterRGB(int rgb, boolean brighter, int percent) {
  138.       int r = rgb >> 16 & 255;
  139.       int g = rgb >> 8 & 255;
  140.       int b = rgb & 255;
  141.       if (brighter) {
  142.          r = 255 - (255 - r) * (100 - percent) / 100;
  143.          g = 255 - (255 - g) * (100 - percent) / 100;
  144.          b = 255 - (255 - b) * (100 - percent) / 100;
  145.       } else {
  146.          r = r * (100 - percent) / 100;
  147.          g = g * (100 - percent) / 100;
  148.          b = b * (100 - percent) / 100;
  149.       }
  150.  
  151.       if (r < 0) {
  152.          r = 0;
  153.       }
  154.  
  155.       if (g < 0) {
  156.          g = 0;
  157.       }
  158.  
  159.       if (b < 0) {
  160.          b = 0;
  161.       }
  162.  
  163.       if (r > 255) {
  164.          r = 255;
  165.       }
  166.  
  167.       if (g > 255) {
  168.          g = 255;
  169.       }
  170.  
  171.       if (b > 255) {
  172.          b = 255;
  173.       }
  174.  
  175.       return rgb & -16777216 | r << 16 | g << 8 | b;
  176.    }
  177. }
  178.